const path = require('path');
const webpack = require('webpack');
const merge = require('webpack-merge');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const VueLoaderPlugin = require('vue-loader/lib/plugin');
const ExtractCssChunks = require('extract-css-chunks-webpack-plugin');

const config = require('./config');

const isProd = process.env.NODE_ENV === 'production';
const version = new Date().getTime();

const webpackBaseConfig = merge(config, {
    output: {
        path: path.resolve(process.cwd(), './dist'),
        filename: `[name].[${isProd ? 'chunkhash' : 'hash'}:8].js`,
        chunkFilename: '[id].js',
        publicPath: '/'
    },
    module: {
        rules: [
            {
                test: /\.vue$/,
                use: ['vue-loader']
            },
            {
                test: /\.(js|jsx)$/,
                use: ['babel-loader'],
                exclude: /node_modules/
            },
            {
                test: /\.css$/,
                oneOf: [
                    {
                        use: [
                            'style-loader',
                            {
                                loader: 'css-loader',
                                options: {
                                    modules: true,
                                    localIdentName: '[local]_[hash:base64:8]'
                                }
                            }
                        ],
                        exclude: /node_modules/
                    },
                    {
                        use: ['style-loader', 'css-loader']
                    }
                ]
            },
            {
                test: /\.json$/,
                use: 'json-loader'
            },
            {
                test: /\.(jpg|png|gif|bmg|svg|ico|jpeg)(\?.*)?$/,
                use: [{
                    loader: 'url-loader',
                    options: {
                        limit: 1,
                        name: 'assets/images/[name].[hash:8].[ext]'
                    }
                }]
            },
            {
                test: /\.(woff|woff2|eot|ttf)(\?.*)?$/,
                use: 'url-loader'
            }
        ]
    },
    plugins: [
        new HtmlWebpackPlugin({
            version,
            filename: 'index.html',
            template: path.join(process.cwd(), './web/index.html'),
            inject: true
        }),
        new VueLoaderPlugin(),
        new webpack.optimize.LimitChunkCountPlugin({
            maxChunks: 1
        })
    ]
})

module.exports = webpackBaseConfig;